//主文件，大量逻辑代码
var UI = {
    init: function () {
        $("#bar_px").button({
            onMousedown: function () {
                var $pxList = $("#px_list"),$barPx = $("#bar_px");
                $pxList.dropdown({
                    target: $barPx, onSelect: function (j, e) {
                        IS_PX=true;
                        Utils.unselect();
                        $("#bar_redo").addClass("disabled");
                        $(".computer-top-nav ").hide();
                        var prevw = Model.define.page.width,
                            prevh = Model.define.page.height,
                            g = parseInt(j.attr("w")),
                            i = parseInt(j.attr("h"));
                        if(isNaN(g)){
                            if($(e.target).hasClass("px-list-custom") || $(e.target).parents(".px-list-custom").length > 0){
                                return "none"
                            }
                            var $el = $(".px-list-custom");
                            if($el.css("display") !== "none"){
                                var style = {
                                    w: "",
                                    h: ""
                                }
                                if(Model.define.page.custom){
                                    style = {
                                        w: Model.define.page.width,
                                        h: Model.define.page.height
                                    }
                                }
                                if(!isNaN(style.w)){
                                    $(".custom-w").val(style.w);
                                }
                                $(".custom-h").val(style.h);
                                $(".px-list-custom-err").removeClass("px-list-custom-err");
                                $(".px-list-custom-errtext").removeClass("px-list-custom-errtext");
                                $el.hide();
                            }else{
                                $el.show();
                            }
                            return "none";

                        }
                        Designer.setPageStyle({ width: g, height: i});
                        $barPx.button("setText", j.text());

                        if(g != prevw || i != prevh){
                            for(var j in Model.define.elements){
                                var el = Model.define.elements[j],
                                    xcount = g / prevw,
                                    ycount = i / prevh,
                                    xsize = el.props.x * xcount,
                                    ysize = el.props.y * ycount;
                                Model.define.elements[j].props.x = xsize;
                                Model.define.elements[j].props.y = ysize;
                                $("#"+ j).css({
                                    "left": xsize + "px",
                                    "top": ysize + "px"
                                });
                            }
                        }

                        Model.define.page.custom = false;
                        $("#canvas_container").css({
                            width: Model.define.page.width,
                            height: Model.define.page.height
                        });
                        Designer.painter.renderShape('', "", true);
                    }
                });
                var f;
                if(Model.define.page.custom){
                    f = $pxList.children("li[w=custom][h=custom]");
                }else{
                    f = $pxList.children("li[w=" + Model.define.page.width + "][h=" + Model.define.page.height + "]");
                }
                $pxList.dropdown("select", f);
            }
        });
        $("#bar_undo").button({
            onClick: function () {
                MessageSource.undo();
            }
        });
        $("#bar_redo").button({
            onClick: function () {
                MessageSource.redo();
            }
        });
        $(document).on("mouseleave", ".shape_box",function () {
            var timer = setTimeout(function () {
                if(!IS_SELECT) {
                    $(".computer-top-nav").hide().attr("data-move", false);
                    $(".component-setting").hide();
                }
            }, 300);
            Model.setTimerOut = timer;
        });
        $(".computer-top-nav").on("mouseenter", function () {
           if (Model.setTimerOut) {
               clearTimeout(Model.setTimerOut);
           }
        }).on("mouseleave", function () {
           if (Model.prevElementId) {
               Model.prevElementId = "";
           }
        });
        //置顶
        $("#bar_front").button({
            onClick: function () {
                $("#bar_redo").addClass("disabled");
                Designer.layerShapes("front");
            }
        });
        //置底
        $("#bar_back").button({
            onClick: function () {
                $("#bar_redo").addClass("disabled");
                Designer.layerShapes("back")
            }
        });
        Designer.events.push("selectChanged", 0);
        Designer.events.push("undoStackChanged", 0);
        Designer.events.push("redoStackChanged", 0);
    },
    update: function (){
        var h = Utils.getSelectedIds();
        var f = h.length;
        if (f == 1) {
            $("#bar_front").button("enable");
            $("#bar_back").button("enable");
        }
        else {
            this.setUpdeteDisable();
        }
    },
    setUpdeteDisable:function(){
        $("#bar_front").button("disable");//置顶
        $("#bar_back").button("disable");//置底
        $("#bar_lock").button("disable");//锁定
    },
    RGB2Hex: function (b) {
        if (b.charAt(0) == "#") {
            return b
        }
        var c = b.split(/\D+/);
        var a = Number(c[1]) * 65536 + Number(c[2]) * 256 + Number(c[3]);
        return "" + UI.toHex(a, 6)
    },
    toHex: function (a, c) {
        var b = a.toString(16);
        while (b.length < c) {
            b = "0" + b;
        }
        return b;
    },
    hex2RGB: function (d) {
        var c = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
        if (d && c.test(d)) {
            if (d.length === 4) {
                var e = "#";
                for (var b = 1; b < 4; b += 1) {
                    e += d.slice(b, b + 1).concat(d.slice(b, b + 1));
                }
                d = e
            }
            var a = [];
            for (var b = 1; b < 7; b += 2) {
                a.push(parseInt("0x" + d.slice(b, b + 2)));
            }
            return a.join(",");
        } else {
            return d;
        }
    }
};
var Dock = {
    init: function () {
        var a = $("#designer_layout").width();
        var d = $("#layout_block").width();
        var c = a - d;
        $("#dock").css("right", c);
        var e = c + $("#dock").outerWidth() - 1;
        $(".dock_view").css("right", e);
        if ($("#demo_signup").length) {
            var b = $("#demo_signup").outerHeight();
            $("#dock").css("top", b);
            $(".dock_view").css("top", b + 10)
        }
        $(".ico_dock_collapse").bind("click", function () {
            //$(".dock_view").hide();
            $(".dock_buttons").children().removeClass("selected");
            if (Dock.currentView == "history") {
                Dock.closeHistory()
            }
            Dock.currentView = "";
            CLB.setConfig("dock", "none")
        });
        $(window).bind("resize.dock", function () {
            if (Dock.currentView == "attribute") {
                Dock.fitAttrList()
            }
        });
        $("#dock_zoom").spinner({
            min: 50, max: 200, unit: "%", step: 10, onChange: function (f) {
                Designer.setZoomScale(f / 100)
            }
        });
        $("#dock_line_color").colorButton({
            onSelect: function (f) {
                Designer.setLineStyle({lineColor: f})
            }
        });
        $("#dock_line_style").button({
            onMousedown: function () {
                $("#line_style_list").dropdown({
                    target: $("#dock_line_style"), onSelect: function (l) {
                        var j = l.attr("line");
                        Designer.setLineStyle({lineStyle: j});
                        var k = l.children("div").attr("class");
                        $("#dock_line_style").children(".linestyle").attr("class", k)
                    }
                });
                var g = Utils.getSelected()[0];
                var f;
                if (g.name == "linker") {
                    f = Utils.getLinkerLineStyle(g.lineStyle)
                } else {
                    f = Utils.getShapeLineStyle(g.lineStyle)
                }
                var h = f.lineStyle;
                var i = $("#line_style_list").children("li[line=" + h + "]");
                $("#line_style_list").dropdown("select", i)
            }
        });
        $("#dock_line_width").spinner({
            min: 0, max: 10, unit: "px", step: 1, onChange: function (f) {
                Designer.setLineStyle({lineWidth: f})
            }
        });
        $("#dock_fill_type").button({
            onMousedown: function () {
                $("#dock_fill_list").dropdown({
                    target: $("#dock_fill_type"), onSelect: function (j) {
                        var i = j.attr("ty");
                        $("#dock_fill_type").button("setText", j.text());
                        if (i == "image") {
                            UI.showImageSelect(function (m, l, n) {
                                Designer.setFillStyle({type: "image", fileId: m, imageW: l, imageH: n})
                            })
                        } else {
                            Designer.setFillStyle({type: i});
                            var k = Utils.getSelectedShapeIds();
                            var h = Model.getShapeById(k[0]);
                            var g = Utils.getShapeFillStyle(h.fillStyle);
                            Dock.setFillStyle(g)
                        }
                    }
                });
                var f = $("#dock_fill_type").text();
                $("#dock_fill_list").children().each(function () {
                    if ($(this).text() == f) {
                        $("#dock_fill_list").dropdown("select", $(this));
                        return false
                    }
                })
            }
        });
        $("#fill_solid_btn").colorButton({
            onSelect: function (f) {
                Designer.setFillStyle({type: "solid", color: f})
            }
        });
        $("#fill_gradient_begin").colorButton({
            onSelect: function (f) {
                Designer.setFillStyle({beginColor: f});
                $("#fill_gradient_begin").attr("c", f)
            }
        });
        $("#fill_gradient_end").colorButton({
            onSelect: function (f) {
                Designer.setFillStyle({endColor: f});
                $("#fill_gradient_end").attr("c", f)
            }
        });
        $("#gradient_swap").button({
            onClick: function () {
                var g = $("#fill_gradient_begin").attr("c");
                var f = $("#fill_gradient_end").attr("c");
                $("#fill_gradient_begin").attr("c", f).colorButton("setColor", f);
                $("#fill_gradient_end").attr("c", g).colorButton("setColor", g);
                Designer.setFillStyle({beginColor: f, endColor: g})
            }
        });
        $("#gradient_type").button({
            onMousedown: function () {
                $("#gradient_type_list").dropdown({
                    target: $("#gradient_type"), onSelect: function (j) {
                        var i = j.attr("ty");
                        $("#gradient_type").button("setText", j.text());
                        Designer.setFillStyle({gradientType: i});
                        $(".gradient_details").hide();
                        $("#gradient_type_" + i).show();
                        var k = Utils.getSelectedShapeIds();
                        var h = Model.getShapeById(k[0]);
                        var g = Utils.getShapeFillStyle(h.fillStyle);
                        if (i == "linear") {
                            $("#gradient_angle").spinner("setValue", Math.round(g.angle / Math.PI * 180) + "°")
                        } else {
                            $("#gradient_radius").spinner("setValue", Math.round(g.radius * 100) + "%")
                        }
                    }
                });
                var f = $("#gradient_type").text().trim();
                $("#gradient_type_list").children().each(function () {
                    if ($(this).text() == f) {
                        $("#gradient_type_list").dropdown("select", $(this));
                        return false
                    }
                })
            }
        });
        $("#gradient_angle").spinner({
            min: 0, max: 360, unit: "°", step: 15, onChange: function (g) {
                var f = g / 180 * Math.PI;
                Designer.setFillStyle({angle: f})
            }
        });
        $("#gradient_radius").spinner({
            min: 0, max: 100, unit: "%", step: 5, onChange: function (f) {
                Designer.setFillStyle({radius: f / 100})
            }
        });
        $("#fill_change_img").button({
            onClick: function () {
                UI.showImageSelect(function (g, f, i) {
                    Designer.setFillStyle({type: "image", fileId: g, imageW: f, imageH: i})
                })
            }
        });
        $("#fill_img_display").button({
            onMousedown: function () {
                $("#img_display_list").dropdown({
                    target: $("#fill_img_display"), onSelect: function (g) {
                        var f = g.attr("ty");
                        $("#fill_img_display").button("setText", g.text());
                        Designer.setFillStyle({display: f})
                    }
                })
            }
        });
        $("#spinner_opacity").spinner({
            min: 0, max: 100, unit: "%", step: 5, onChange: function (f) {
                Designer.setShapeStyle({alpha: f / 100})
            }
        });
        $("#dock_metric_x").spinner({
            min: -800, unit: "px", step: 5, onChange: function (f) {
                Designer.setShapeProps({x: f})
            }
        });
        $("#dock_metric_x").spinner("setValue", "0px");
        $("#dock_metric_w").spinner({
            min: 20, unit: "px", step: 5, onChange: function (f) {
                Designer.setShapeProps({w: f})
            }
        });
        $("#dock_metric_y").spinner({
            min: -800, unit: "px", step: 5, onChange: function (f) {
                Designer.setShapeProps({y: f})
            }
        });
        $("#dock_metric_y").spinner("setValue", "0px");
        $("#dock_metric_h").spinner({
            min: 20, unit: "px", step: 5, onChange: function (f) {
                Designer.setShapeProps({h: f})
            }
        });
        $("#dock_metric_angle").spinner({
            min: 0, max: 360, unit: "°", step: 15, onChange: function (g) {
                var f = g / 180 * Math.PI;
                Designer.setShapeProps({angle: f})
            }
        });
        $("#dock_page_size").button({
            onMousedown: function () {
                $("#page_size_list").dropdown({
                    target: $("#dock_page_size"), onSelect: function (j) {
                        var g = parseInt(j.attr("w"));
                        var i = parseInt(j.attr("h"));
                        Designer.setPageStyle({width: g, height: i});
                        $("#dock_page_size").button("setText", j.text())
                    }
                });
                var f = $("#page_size_list").children("li[w=" + Model.define.page.width + "][h=" + Model.define.page.height + "]");
                if (f.length > 0) {
                    $("#page_size_list").dropdown("select", f)
                } else {
                    $("#page_size_list").dropdown("select", $("#dock_size_custom"))
                }
                $("#dock_size_w").spinner("setValue", Model.define.page.width + "px");
                $("#dock_size_h").spinner("setValue", Model.define.page.height + "px")
            }
        });
        $("#dock_size_w").spinner({
            min: 200, unit: "px", step: 100, onChange: function (f) {
                Designer.setPageStyle({width: f})
            }
        });
        $("#dock_size_h").spinner({
            min: 200, unit: "px", step: 100, onChange: function (f) {
                Designer.setPageStyle({height: f})
            }
        });
        $("#dock_page_padding").button({
            onMousedown: function () {
                $("#page_padding_list").dropdown({
                    target: $("#dock_page_padding"), onSelect: function (g) {
                        var h = parseInt(g.attr("p"));
                        Designer.setPageStyle({padding: h});
                        $("#dock_page_padding").button("setText", g.text())
                    }
                });
                var f = $("#page_padding_list").children("li[p=" + Model.define.page.padding + "]");
                $("#page_padding_list").dropdown("select", f)
            }
        });
        $("#dock_page_color").colorButton({
            position: "center", onSelect: function (f) {
                Designer.setPageStyle({backgroundColor: f})
            }
        });
        $(".dock_page_ori_list").children("input").unbind().bind("click", function () {
            var f = $(this).val();
            Designer.setPageStyle({orientation: f})
        });
        $("#dock_page_showgrid").bind("change", function () {
            var f = $(this).is(":checked");
            Designer.setPageStyle({showGrid: f});
            if (f) {
                $("#dock_gridsize_box").show()
            } else {
                $("#dock_gridsize_box").hide()
            }
        });
        $("#dock_page_gridsize").button({
            onMousedown: function () {
                $("#page_gridsize_list").dropdown({
                    target: $("#dock_page_gridsize"), onSelect: function (h) {
                        var g = parseInt(h.attr("s"));
                        Designer.setPageStyle({gridSize: g});
                        $("#dock_page_gridsize").button("setText", h.text())
                    }
                });
                var f = $("#page_gridsize_list").children("li[s=" + Model.define.page.gridSize + "]");
                $("#page_gridsize_list").dropdown("select", f)
            }
        });
        $("#btn_history_add").button({
            onClick: function () {
                Dock.toggleAddHistory()
            }
        });
        $("#btn_history_restore").button({
            onClick: function () {
                Dock.restoreVersion()
            }
        });
        $("#txt_sub_comment").bind("keyup", function (j) {
            if (j.keyCode == 13) {
                var f = $(this);
                var i = f.val().replace(/\n/g, "");
                if (i.trim() == "") {
                    return
                }
                var l = "";
                var h = Utils.getSelectedShapeIds();
                if (h.length == 1) {
                    l = h[0]
                }
                var k = {
                    action: "comment",
                    userId: userId,
                    id: Utils.newId(),
                    name: userName,
                    content: i,
                    shapeId: l,
                    replyId: ""
                };
                CLB.send(k);
                Dock.appendComment(k);
                Dock.bindComment();
                $("#comment_container").scrollTop(9999);
                f.val("");
                if (!Model.comments) {
                    Model.comments = []
                }
                Model.comments.push(k);
                if (l != "") {
                    var g = Model.getShapeById(l);
                    Designer.painter.renderShape(g)
                }
            }
        });
        $("#show_comment_ico").bind("click", function (h) {
            var f = $("#show_comment_ico").is(":checked");
            CLB.setConfig("showCommentIco", f);
            showCommentIco = f;
            for (var i in Model.define.elements) {
                var g = Model.getShapeById(i);
                if (g.name != "linker") {
                    Designer.painter.renderShape(g)
                }
            }
        });
        if (dock != "none") {
            if (dock == "") {
                dock = "navigator"
            }
            this.showView(dock)
        }
    }, currentView: "", showView: function (a, b) {
        if ($("#dock_btn_" + a).button("isDisabled")) {
            return
        }
        //$(".dock_view").hide();
        $(".dock_view_" + a).show();
        $(".dock_buttons").children().removeClass("selected");
        $("#dock_btn_" + a).addClass("selected");
        if (Dock.currentView == "history" && a != "history") {
            Dock.closeHistory()
        }
        this.currentView = a;
        this.update(true);
        if (b) {
            CLB.setConfig("dock", a)
        }
    }, setFillStyle: function (a) {
        $("#dock_fill_type").button("setText", $("#dock_fill_list").children("li[ty=" + a.type + "]").text());
        $(".fill_detail").hide();
        if (a.type == "solid") {
            $(".fill_detail_solid").show();
            $("#fill_solid_btn").colorButton("setColor", a.color)
        } else {
            if (a.type == "gradient") {
                $(".fill_detail_gradient").show();
                $("#fill_gradient_begin").attr("c", a.beginColor).colorButton("setColor", a.beginColor);
                $("#fill_gradient_end").attr("c", a.endColor).colorButton("setColor", a.endColor);
                $("#gradient_type").button("setText", $("#gradient_type_list").children("li[ty=" + a.gradientType + "]").text());
                $(".gradient_details").hide();
                if (a.gradientType == "linear") {
                    $("#gradient_type_linear").show();
                    $("#gradient_angle").spinner("setValue", Math.round(a.angle / Math.PI * 180) + "°")
                } else {
                    $("#gradient_type_radial").show();
                    $("#gradient_radius").spinner("setValue", Math.round(a.radius * 100) + "%")
                }
            } else {
                if (a.type == "image") {
                    $(".fill_detail_image").show();
                    var b = "fill";
                    if (a.display) {
                        b = a.display
                    }
                    $("#fill_img_display").button("setText", $("#img_display_list").children("li[ty=" + b + "]").text())
                }
            }
        }
    }, update: function (o) {
        if (this.currentView == "navigator") {
            if (o) {
                Navigator.draw()
            }
            $("#dock_zoom").spinner("setValue", Math.round(Designer.config.scale * 100) + "%")
        } else {
            if (this.currentView == "graphic") {
                var l = Utils.getSelectedIds();
                var k = l.length;
                var i = Utils.getSelectedShapeIds();
                var r = i.length;
                if (k == 0) {
                    $("#dock_line_color").button("disable");
                    $("#dock_line_style").button("disable");
                    $("#dock_line_width").button("disable")
                } else {
                    $("#dock_line_color").button("enable");
                    $("#dock_line_style").button("enable");
                    $("#dock_line_width").button("enable");
                    var m = Model.getShapeById(l[0]);
                    var j;
                    if(!!m){
                        if (m.name == "linker") {
                            j = Utils.getLinkerLineStyle(m.lineStyle)
                        } else {
                            j = Utils.getShapeLineStyle(m.lineStyle)
                        }
                        $("#dock_line_color").colorButton("setColor", j.lineColor);
                        var f = $("#line_style_list").children("li[line=" + j.lineStyle + "]").children().attr("class");
                        $("#dock_line_style").children(".linestyle").attr("class", f);
                        $("#dock_line_width").spinner("setValue", j.lineWidth + "px")
                    }

                }
                if (r == 0) {
                    $("#dock_fill_type").button("disable");
                    $("#spinner_opacity").button("disable");
                    Dock.setFillStyle({type: "none"})
                } else {
                    $("#dock_fill_type").button("enable");
                    $("#spinner_opacity").button("enable");
                    var m = Model.getShapeById(i[0]);
                    var q = Utils.getShapeFillStyle(m.fillStyle);
                    Dock.setFillStyle(q);
                    $("#spinner_opacity").spinner("setValue", Math.round(m.shapeStyle.alpha / 1 * 100) + "%")
                }
            } else {
                if (this.currentView == "metric") {
                    var i = Utils.getSelectedShapeIds();
                    var r = i.length;
                    if (r == 0) {
                        $("#dock_metric_x").button("disable");
                        $("#dock_metric_w").button("disable");
                        $("#dock_metric_y").button("disable");
                        $("#dock_metric_h").button("disable");
                        $("#dock_metric_angle").button("disable")
                    } else {
                        var m = Model.getShapeById(i[0]);
                        $("#dock_metric_x").button("enable").spinner("setValue", Math.round(m.props.x) + "px");
                        $("#dock_metric_w").button("enable").spinner("setValue", Math.round(m.props.w) + "px");
                        $("#dock_metric_y").button("enable").spinner("setValue", Math.round(m.props.y) + "px");
                        $("#dock_metric_h").button("enable").spinner("setValue", Math.round(m.props.h) + "px");
                        $("#dock_metric_angle").button("enable").spinner("setValue", Math.round(m.props.angle / Math.PI * 180) + "°")
                    }
                } else {
                    if (this.currentView == "page") {
                        var n = Model.define.page;
                        var p = n.width;
                        var g = n.height;
                        var e = $("#page_size_list").children("li[w=" + p + "][h=" + g + "]");
                        var d = "";
                        if (e.length > 0) {
                            d = e.text()
                        } else {
                            d = $("#dock_size_custom").text()
                        }
                        $("#dock_page_size").button("setText", d);
                        $("#dock_page_padding").button("setText", n.padding + "px");
                        $("#dock_page_color").colorButton("setColor", n.backgroundColor);
                        $("#dock_page_showgrid").attr("checked", n.showGrid);
                        if (n.showGrid) {
                            $("#dock_gridsize_box").show()
                        } else {
                            $("#dock_gridsize_box").hide()
                        }
                        var b = "";
                        var c = $("#page_gridsize_list").children("li[s=" + n.gridSize + "]");
                        if (c.length > 0) {
                            var b = c.text()
                        }
                        $("#dock_page_gridsize").button("setText", b);
                        var a = "portrait";
                        if (Model.define.page.orientation) {
                            a = Model.define.page.orientation
                        }
                        $(".dock_page_ori_list").children("input[value=" + a + "]").attr("checked", true)
                    } else {
                        if (this.currentView == "attribute") {
                            var l = Utils.getSelectedIds();
                            var k = l.length;
                            if (k != 1) {
                                $(".attr_list").html("<li class='attr_none'>选择一个图形后，在这里查看数据属性</li>");
                                $(".attr_add").hide();
                                this.fitAttrList()
                            } else {
                                this.setAttributeList();
                                $(".attr_add").show();
                                this.cancelAttrAdd()
                            }
                        } else {
                            if (this.currentView == "history") {
                                if (o && Dock.historyVersions == null) {
                                    this.loadHistorys()
                                }
                            } else {
                                if (this.currentView == "comment") {
                                    this.loadComments();
                                    if (showCommentIco) {
                                        $("#show_comment_ico").attr("checked", true)
                                    } else {
                                        $("#show_comment_ico").attr("checked", false)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }, historyVersions: null, loadHistorys: function () {
        if (chartId == "") {
            $("#history_container").html("<div style='padding: 20px 10px;'>您正在试用状态，无法浏览历史版本</div>");
            return
        }
    }, resetVersions: function () {
        if ($("#history_versions").length == 0) {
            return
        }
        $("#history_versions").children("li").unbind().bind("click", function () {
            if ($(this).hasClass("selected")) {
                Dock.closeHistory()
            } else {
                $("#history_versions").children(".selected").removeClass("selected");
                $(this).addClass("selected");
                var d = $(this).attr("def");
                Dock.showHistoryVersion(d)
            }
            var e = $("#history_versions").children(".selected");
            if (e.length != 0 && e.attr("ind") != "0") {
                $("#btn_history_restore").button("enable")
            } else {
                $("#btn_history_restore").button("disable")
            }
        });
        $("#history_versions").height("auto");
        var c = $("#history_versions").offset().top;
        var b = c + $("#history_versions").height() + 75;
        if (b > $(window).height()) {
            var a = $(window).height() - c - 75;
            if (a < 140) {
                a = 140
            }
            $("#history_versions").height(a)
        } else {
            $("#history_versions").height("auto")
        }
        $("#history_versions").scrollTop(0)
    }, toggleAddHistory: function () {
        $(".area_history").toggle();
        if ($("#area_history_add").is(":visible")) {
            $("#history_remark").focus()
        }
    }, addHistory: function () {
        if (chartId != "") {
            var b = $("#history_remark").val();
            var a = {action: "addHistory", remark: b};
            CLB.sendDirectly(a, function () {
                Dock.loadHistorys();
                $("#history_remark").val("");
                Dock.toggleAddHistory()
            })
        }
    }, initRemoveHistory: function () {
        function a() {
            $(".dock_history_remove_div").find("span:first").off().on("click", function (c) {
                var b = $(this).parents("li").attr("vid");
                if (chartId != "") {
                    var d = {action: "removeHistory", vid: b};
                    CLB.sendDirectly(d, function () {
                        Dock.loadHistorys()
                    })
                }
                c.stopPropagation()
            });
            $(".dock_history_remove_div").find("span:last").off().on("click", function (b) {
                $(".dock_history_remove_con").remove();
                b.stopPropagation()
            })
        }

        $(".dock_history_remove").off().on("click", function (f) {
            $(".dock_history_remove_con").remove();
            var c = $(this).parent().parent();
            var b = c.attr("vid");
            var d = $("<div class='dock_history_remove_con'><div class='dock_history_remove_div'>确认删除此历史版本？<div style='margin-top:8px;'><span class='toolbar_button active'>确定</span>&nbsp;&nbsp;<span class='toolbar_button active'>取消</span></div></div></div>");
            d.appendTo(c);
            a();
            f.stopPropagation()
        })
    }, showHistoryVersion: function (a) {
        $("#btn_history_restore").button("disable");
    }, currentDefinition: null, openHistory: function (a) {
        if (this.currentDefinition == null) {
            this.currentDefinition = $.extend(true, {}, Model.define)
        }
        Utils.unselect();
        Designer.open(a);
        Designer.hotkey.cancel();
        Designer.op.cancel();
        $("#menu_bar").children().addClass("readonly");
        $(".diagram_title").addClass("readonly");
        $(".dock_buttons").children().addClass("disabled");
        $("#dock_btn_history").removeClass("disabled");
        $(".panel_box").addClass("readonly");
        CLB.stopListen()
    }, closeHistory: function () {
        if (this.currentDefinition != null) {
            Designer.open(this.currentDefinition);
            this.currentDefinition = null;
            this.activeOperation();
            UI.hideTip()
        }
    }, activeOperation: function () {
        Designer.hotkey.init();
        Designer.op.init();
        $("#menu_bar").children().removeClass("readonly");
        $(".diagram_title").removeClass("readonly");
        $(".dock_buttons").children().removeClass("disabled");
        $("#dock_btn_history").removeClass("disabled");
        $(".panel_box").removeClass("readonly");
        $("#history_versions").children(".selected").removeClass("selected");
        $("#btn_history_restore").button("disable");
        CLB.startListen()
    }, restoreVersion: function () {
        var d = $("#history_versions").children(".selected");
        if (d.length) {
            MessageSource.beginBatch();
            var e = Dock.currentDefinition.elements;
            var f = [];
            if (e) {
                for (var h in e) {
                    f.push(e[h])
                }
            }
            MessageSource.send("remove", f);
            var b = {page: Utils.copy(Dock.currentDefinition.page), update: Utils.copy(Model.define.page)};
            MessageSource.send("updatePage", b);
            var a = Model.define.elements;
            var c = [];
            if (a) {
                for (var h in a) {
                    c.push(a[h])
                }
            }
            MessageSource.send("create", c);
            var g = {theme: Utils.copy(Dock.currentDefinition.theme), update: Utils.copy(Model.define.theme)};
            MessageSource.send("setTheme", g);
            MessageSource.commit();
            Dock.activeOperation();
            UI.hideTip()
        }
    }, currentCommentId: "-1", commentOperate: "", loadComments: function () {
        if (this.commentOperate == "viewing_comment") {
            return
        }
        var d = $("#comment_container");
        var c = Utils.getSelectedShapeIds();
        var e;
        if (c.length == 1) {
            e = c[0]
        } else {
            e = ""
        }
        if (this.currentCommentId != e) {
            this.currentCommentId = e;
            if (!Model.comments || Model.comments.length == 0) {
                d.html("<div class='comment_none'>暂时没有评论</div>")
            } else {
                d.empty();
                for (var b = 0; b < Model.comments.length; b++) {
                    var a = Model.comments[b];
                    if (a.shapeId == e || e == "") {
                        Dock.appendComment(a)
                    }
                }
                Dock.bindComment();
                Dock.fitComments()
            }
        }
    }, bindComment: function () {
        var a = $("#comment_container");
        a.find("input").unbind("click").bind("click", function (b) {
            b.stopPropagation()
        });
        a.find(".input_comment_reply").unbind("keydown").bind("keydown", function (d) {
            if (d.keyCode == 13) {
                var c = $(this).val();
                if (c == "") {
                    return
                }
                $(this).val("");
                var b = $(this).parent().parent().parent().attr("id");
                var f = {
                    action: "comment",
                    userId: userId,
                    id: Utils.newId(),
                    name: userName,
                    content: c,
                    shapeId: "",
                    replyId: b
                };
                CLB.send(f);
                Dock.appendComment(f);
                Dock.bindComment()
            }
            Dock.fitComments()
        });
        a.find(".comment_remove").unbind("click").bind("click", function (g) {
            g.stopPropagation();
            $(this).parent().remove();
            var k = $(this).parent().attr("id");
            var h = {action: "removeComment", id: k};
            CLB.send(h);
            Dock.fitComments();
            var f = [];
            for (var d = 0; d < Model.comments.length; d++) {
                var c = Model.comments[d];
                if (c.id != k && c.replyId != k) {
                    f.push(c)
                }
            }
            Model.comments = f;
            var j = $(this).parent().attr("shapeId");
            var b = Model.getShapeById(j);
            if (b != null && b.name != "linker") {
                Designer.painter.renderShape(b)
            }
        });
        a.children(".comment_item_outer").unbind().bind("click", function () {
            if ($(this).hasClass("selected")) {
                $(this).removeClass("selected")
            } else {
                a.children(".selected").removeClass("selected");
                $(this).addClass("selected")
            }
            var c = $(this).attr("shapeId");
            Dock.commentOperate = "viewing_comment";
            if (c) {
                var b = Model.getShapeById(c);
                if (b != null) {
                    Utils.selectShape(c)
                }
            }
            Dock.fitComments();
            Dock.commentOperate = ""
        })
    }, fitComments: function () {
        var c = $("#comment_container");
        var e = c.scrollTop();
        c.height("auto");
        var d = c.offset().top;
        var b = d + c.height() + 145;
        if (b > $(window).height()) {
            var a = $(window).height() - d - 145;
            if (a < 120) {
                a = 120
            }
            c.height(a)
        } else {
            c.height("auto")
        }
        c.scrollTop(e)
    }, appendComment: function (a) {
        $(".comment_none").remove();
        var b = $("<div class='comment_item' id='" + a.id + "'></div>").appendTo(e);
        if (a.replyId) {
            var e = $("#comment_container").children("#" + a.replyId + ".comment_item").find(".comment_reply_list");
            b = $("<div class='comment_item comment_item_inner' id='" + a.id + "'></div>").appendTo(e)
        } else {
            var e = $("#comment_container");
            b = $("<div class='comment_item comment_item_outer' id='" + a.id + "'></div>").appendTo(e)
        }
        if (role == "owner" || a.userId == userId) {
            b.append("<div class='ico comment_remove' disableTitle='true' title='删除'></div>")
        }
        if (a.shapeId) {
            b.attr("shapeId", a.shapeId)
        }
        b.append("<img src='/photo/" + a.userId + ".png'/>");
        var c = $("<div class='comment_content_box'></div>").appendTo(b);
        c.append("<div class='comment_name'>" + a.name + "</div>");
        c.append("<div class='comment_content'>" + a.content + "</div>");
        var g = new Date();
        if (a.time) {
            g.setTime(a.time)
        }
        var f = g.format(dateFormat);
        c.append("<div class='comment_date'>" + f + "</div>");
        if (!a.replyId) {
            c.append("<div class='comment_reply_list'></div>");
            c.append("<div class='comment_reply'><input type='text' class='input_text input_comment_reply' placeholder='回复此评论...'/></div>")
        }
        Dock.fitComments()
    }, setAttributeList: function () {
        var c = Utils.getSelectedIds();
        var b = Model.getShapeById(c[0]);
        $(".attr_list").empty();
        if (b.dataAttributes) {
            for (var d = 0; d < b.dataAttributes.length; d++) {
                var a = b.dataAttributes[d];
                var f = $("#attr_add_type").children("option[value=" + a.type + "]").text();
                var e = $("<li id='" + a.id + "' class='attr_item attr_item_" + a.id + "' onclick=\"Dock.editAttr('" + a.id + "')\"><div class='attr_name'>" + a.name + "</div><div class='attr_type'>" + f + "</div><div class='attr_value'>" + a.value + "</div><div style='clear: both'></div></li>").appendTo($(".attr_list"));
                if (a.category != "default") {
                    e.append("<div class='ico ico_attr_delete' onclick=\"Dock.deleteAttr('" + a.id + "', event)\"></div>")
                }
            }
        }
        this.fitAttrList()
    }, fitAttrList: function () {
        var b = $(".attr_list").scrollTop();
        $(".attr_list").height("auto");
        var d = $(".attr_list").offset().top;
        var c = d + $(".attr_list").height() + 10;
        if (c > $(window).height()) {
            var a = $(window).height() - d - 10;
            if (a < 140) {
                a = 140
            }
            $(".attr_list").height(a)
        } else {
            $(".attr_list").height("auto")
        }
        $(".attr_list").scrollTop(b)
    }, showAttrAdd: function () {
        $("#attr_add_btn").hide();
        $(".attr_add_items").show();
        $("#attr_add_name").val("").focus();
        $("#attr_add_type").val("string");
        $("#attr_add_type").unbind().bind("change", function () {
            Dock.setAttrValueInput(null, $(this).val())
        });
        Dock.setAttrValueInput(null, "string");
        this.fitAttrList()
    }, saveAttrAdd: function () {
        var a = $("#attr_add_name").val();
        if (a == "") {
            $("#attr_add_name").focus();
            return
        }
        var b = $("#attr_add_type").val();
        var c = $("#attr_add_value_arera").children().val();
        var d = {name: a, type: b, value: c};
        Designer.addDataAttribute(d);
        this.setAttributeList();
        this.showAttrAdd()
    }, cancelAttrAdd: function () {
        $("#attr_add_btn").show();
        $(".attr_add_items").hide();
        this.fitAttrList()
    }, editAttr: function (f) {
        var m = $(".attr_item_" + f);
        if (m.hasClass("attr_editing")) {
            return
        }
        if ($(".attr_editing").length > 0) {
            var c = $(".attr_editing").attr("id");
            this.saveAttrEdit(c)
        }
        m = $(".attr_item_" + f);
        m.addClass("attr_editing");
        var g = Designer.getDataAttrById(f);
        var j = this.setAttrValueInput(g, g.type);
        j.val(g.value).select();
        if (g.category != "default") {
            var h = m.children(".attr_name");
            h.empty();
            var l = $("<input type='text' class='input_text' style='width: 88px'/>").appendTo(h);
            l.val(g.name).select();
            var b = m.children(".attr_type");
            b.empty();
            var i = $("<select class='input_select' style='width: 60px'></select>").appendTo(b);
            i.html($("#attr_add_type").html()).val(g.type);
            i.bind("change", function () {
                Dock.setAttrValueInput(g, $(this).val())
            })
        }
        var k = $("<div class='attr_edit_display'></div>").appendTo(m);
        k.append("<div class='dock_label'>显示为：</div>");
        k.append("<div id='attr_edit_showtype' class='toolbar_button active btn_inline' style='width: 75px;'><div class='text_content'></div><div class='ico ico_dropdown'></div></div>");
        k.append("<div style='clear: both'></div>");
        k.append("<div class='attr_display_options'></div>");
        this.appendDisplayItems();
        var e = "none";
        if (g.showType) {
            e = g.showType
        }
        this.setAttrDisplay(e);
        $("#attr_edit_showtype").attr("ty", e).button({
            onMousedown: function () {
                $("#attr_display_list").dropdown({
                    target: $("#attr_edit_showtype"), onSelect: function (p) {
                        var o = p.attr("ty");
                        $("#attr_edit_showtype").attr("ty", o).button("setText", p.text());
                        Dock.setAttrDisplay(o)
                    }
                });
                var n = $("#attr_edit_showtype").text().trim();
                $("#attr_display_list").children().each(function () {
                    if ($(this).text() == n) {
                        $("#attr_display_list").dropdown("select", $(this));
                        return false
                    }
                })
            }
        });
        $("#attr_edit_showtype").attr("ty", e).button("setText", $("#attr_display_list").children("li[ty=" + e + "]").html());
        if (e != "none") {
            $("#attr_display_name").attr("checked", g.showName);
            if (e == "icon") {
                this.setAttrIcon(g.icon)
            }
        }
        var a = "mostright";
        if (g.horizontal) {
            a = g.horizontal
        }
        var d = "mostbottom";
        if (g.vertical) {
            d = g.vertical
        }
        $("#attr_location_h").button("setText", $("#attr_location_h_list").children("li[loc=" + a + "]").html());
        $("#attr_location_h").attr("loc", a);
        $("#attr_location_v").button("setText", $("#attr_location_v_list").children("li[loc=" + d + "]").html());
        $("#attr_location_v").attr("loc", d);
        m.append("<div class='attr_edit_btns'><div id='save_edit_attr' class='toolbar_button active'>确定</div><div id='cancel_edit_attr' class='toolbar_button active' style='margin-left: 5px;'>取消</div></div>");
        $("#save_edit_attr").bind("click", function (n) {
            n.stopPropagation();
            Dock.saveAttrEdit(f)
        });
        $("#cancel_edit_attr").bind("click", function (n) {
            n.stopPropagation();
            Dock.setAttributeList()
        })
    }, setAttrValueInput: function (c, e) {
        var b;
        if (c != null) {
            b = $(".attr_editing").children(".attr_value")
        } else {
            b = $("#attr_add_value_arera")
        }
        b.empty();
        var a;
        if (e == "boolean") {
            a = $("<select class='input_select'><option value=''></option><option value='true'>true</option><option value='false'>false</option></select>").appendTo(b)
        } else {
            if (e == "list") {
                a = $("<select class='input_select'></select>").appendTo(b);
                if (c.listItems) {
                    for (var d = 0; d < c.listItems.length; d++) {
                        var f = c.listItems[d];
                        a.append("<option value='" + f + "'>" + f + "</option>")
                    }
                }
            } else {
                a = $("<input type='text' class='input_text'/>").appendTo(b)
            }
        }
        if (c == null) {
            b.children().css("width", "260px")
        } else {
            b.children().css("width", "128px")
        }
        return a
    }, appendDisplayItems: function () {
        var e = $(".attr_display_options");
        var f = $("<div class='opt_area'></div>").appendTo(e);
        f.append("<input id='attr_display_name' type='checkbox'/><label for='attr_display_name'>显示属性名</label>");
        var d = $("<div id='attr_icon_area' style='padding-top:5px;'></div>").appendTo(f);
        d.append("<div class='dock_label'>图标：</div>");
        d.append("<div id='attr_display_icon' ico='' class='toolbar_button active btn_inline' style='width: 50px'><div class='text_content'></div><div class='ico ico_dropdown'></div></div>");
        d.append("<div style='clear: both'></div>");
        if ($("#attr_icon_list").children("li").html() == "") {
            var b = "";
            var a = 1;
            while (a <= 49) {
                if (a == 30) {
                    b += "<div></div>"
                }
                b += "<div onmousedown='Dock.setAttrIcon(" + a + ")' class='attr_icon_item'></div>";
                a++
            }
            $("#attr_icon_list").children("li").html(b)
        }
        var c = $("<div class='opt_area location_area'></div>").appendTo(e);
        c.append("<div>显示位置：</div>");
        c.append("<div class='dock_label'>水平：</div>");
        c.append("<div id='attr_location_h' class='toolbar_button active btn_inline' loc='mostright'><div class='text_content location_content'><div><span style='left: 11px'></span></div>Most Right</div><div class='ico ico_dropdown'></div></div>");
        c.append("<div style='clear: both'></div>");
        c.append("<div class='dock_label'>垂直：</div>");
        c.append("<div id='attr_location_v' class='toolbar_button active btn_inline' loc='mostbottom'><div class='text_content location_content'><div><span style='top: 11px'></span></div>Most Bottom</div><div class='ico ico_dropdown'></div></div>");
        c.append("<div style='clear: both'></div>");
        e.append("<div style='clear: both'></div>");
        $("#attr_display_icon").button({
            onMousedown: function () {
                $("#attr_icon_list").dropdown({target: $("#attr_display_icon")})
            }
        });
        $("#attr_location_h").button({
            onMousedown: function () {
                $("#attr_location_h_list").dropdown({
                    target: $("#attr_location_h"), onSelect: function (g) {
                        $("#attr_location_h").button("setText", g.html());
                        $("#attr_location_h").attr("loc", g.attr("loc"))
                    }
                })
            }
        });
        $("#attr_location_v").button({
            onMousedown: function () {
                $("#attr_location_v_list").dropdown({
                    target: $("#attr_location_v"), onSelect: function (g) {
                        $("#attr_location_v").button("setText", g.html());
                        $("#attr_location_v").attr("loc", g.attr("loc"))
                    }
                })
            }
        })
    }, setAttrDisplay: function (a) {
        if (a == "none") {
            $(".attr_display_options").hide()
        } else {
            $(".attr_display_options").show();
            if (a == "icon") {
                $("#attr_icon_area").show()
            } else {
                $("#attr_icon_area").hide()
            }
        }
    }, setAttrIcon: function (a) {
        $("#attr_display_icon").attr("ico", a).button("setText", "");
        if (a) {
            $("#attr_display_icon").button("setText", "<img src='/images/data-attr/" + a + ".png'/>")
        }
    }, saveAttrEdit: function (f) {
        var j = $(".attr_item_" + f);
        if (!j.hasClass("attr_editing")) {
            return
        }
        var i = Designer.getDataAttrById(f);
        if (i.category != "default") {
            var a = j.children(".attr_name").children("input").val();
            if (a == "") {
                j.children(".attr_name").children("input").focus();
                return
            }
            i.name = a;
            i.type = j.children(".attr_type").children("select").val()
        }
        i.value = j.children(".attr_value").children().val();
        var d = $("#attr_edit_showtype").attr("ty");
        i.showType = d;
        if (d != "none") {
            i.showName = $("#attr_display_name").is(":checked");
            i.horizontal = $("#attr_location_h").attr("loc");
            i.vertical = $("#attr_location_v").attr("loc");
            if (d == "icon") {
                i.icon = $("#attr_display_icon").attr("ico")
            }
        }
        var g = Utils.getSelectedIds();
        var h = Model.getShapeById(g[0]);
        if (i.category == "default" && h.category == "bpmn") {
            if (!h.attribute) {
                h.attribute = {}
            }
            if (!h.attribute.markers) {
                h.attribute.markers = []
            }
            var c = h.attribute.markers;
            if (i.name == "loopCharacteristics" || i.name == "循环特征") {
                Utils.removeFromArray(c, "loop");
                Utils.removeFromArray(c, "sequential");
                Utils.removeFromArray(c, "parallel");
                if (i.value == "StandardLoopCharacteristics" || i.value == "标准") {
                    Utils.addToArray(c, "loop")
                } else {
                    if (i.value == "MultipleLoopCharacteristics" || i.value == "多例") {
                        var b = Designer.getDefaultDataAttrByName("isSequantial");
                        if (b == null) {
                            b = Designer.getDefaultDataAttrByName("是否为序列")
                        }
                        if (b != null) {
                            if (b.value == "true") {
                                Utils.addToArray(c, "sequential")
                            } else {
                                Utils.addToArray(c, "parallel")
                            }
                        }
                    }
                }
            } else {
                if (i.name == "isSequantial" || i.name == "是否为序列") {
                    Utils.removeFromArray(c, "sequential");
                    Utils.removeFromArray(c, "parallel");
                    var e = Designer.getDefaultDataAttrByName("loopCharacteristics");
                    if (e == null) {
                        e = Designer.getDefaultDataAttrByName("循环特征")
                    }
                    if (e != null && (e.value == "MultipleLoopCharacteristics" || e.value == "多例")) {
                        if (i.value == "true") {
                            Utils.addToArray(c, "sequential")
                        } else {
                            Utils.addToArray(c, "parallel")
                        }
                    }
                } else {
                    if (i.name == "isForCompensation" || i.name == "是否为补偿") {
                        Utils.removeFromArray(c, "compensation");
                        if (i.value == "true") {
                            Utils.addToArray(c, "compensation")
                        }
                    } else {
                        if (i.name == "isCollection" || i.name == "participantMultiplicity" || i.name == "是否为集合" || i.name == "多重参与者") {
                            Utils.removeFromArray(c, "parallel");
                            if (i.value == "true") {
                                Utils.addToArray(c, "parallel")
                            }
                        } else {
                            if (i.name == "loopType" || i.name == "循环类型") {
                                Utils.removeFromArray(c, "loop");
                                Utils.removeFromArray(c, "sequential");
                                Utils.removeFromArray(c, "parallel");
                                if (i.value == "Standard" || i.value == "标准") {
                                    Utils.addToArray(c, "loop")
                                } else {
                                    if (i.value == "MultiInstanceSequential" || i.value == "实例化多例序列") {
                                        Utils.addToArray(c, "sequential")
                                    } else {
                                        if (i.value == "MultiInstanceParallel" || i.value == "实例化多例并行") {
                                            Utils.addToArray(c, "parallel")
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Designer.updateDataAttribute(i);
        this.setAttributeList()
    }, deleteAttr: function (c, b) {
        b.stopPropagation();
        var a = $(".attr_item_" + c);
        a.remove();
        this.fitAttrList();
        Designer.deleteDataAttribute(c)
    }, fullScreen: function (a, b) {
        if (a.requestFullscreen) {
            a.requestFullscreen()
        } else {
            if (a.mozRequestFullScreen) {
                a.mozRequestFullScreen()
            } else {
                if (a.webkitRequestFullscreen) {
                    a.webkitRequestFullscreen()
                } else {
                    if (b) {
                        $("#fullscreen_tip").find(".t").text("由于您的浏览器限制，无法进入演示视图。")
                    } else {
                        $("#fullscreen_tip").find(".t").text("无法进入全屏视图，您可以按(F11)进入。")
                    }
                    $("#fullscreen_tip").fadeIn()
                }
            }
        }
    }, enterPresentation: function () {   
        $("#designer").bind("webkitfullscreenchange", function (a) {
            Dock.manageFullScreen()
        });
        $(document).bind("mozfullscreenchange", function (a) {
            Dock.manageFullScreen()
        }).bind("fullscreenchange", function (a) {
            Dock.manageFullScreen()
        });
        this.fullScreen(Utils.getDomById("designer"), true)
    }, enterFullScreen: function () {
        this.fullScreen(document.documentElement)
    }, manageFullScreen: function () {
        var a = Utils.getDomById("designer");
        var $elcon = $("#designer_viewport"),
            fullexitel = $(".fullexit");
        if (document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement) {
            if(parseInt($elcon.css("margin-left")) > 0){
                $elcon.attr("data-margin", true).css({
                    "margin-left": 0,
                    position: "absolute",
                    top: 0
                });
            }
            fullexitel.show();
            $("#shape_panel").addClass("readonly");
            $("#designer_viewport").addClass("readonly");
            $(".shape-panel-btn").hide();
            //$(window).unbind("resize.designer");
            $("#designer_layout").height(window.screen.height).width(window.screen.width);
            IS_FULL_SCREEN = true;
            thumbnail.updataSize({
                width: Model.define.page.width,
                height: Model.define.page.height
            }, true);

            //Designer.hotkey.cancel();
            //Designer.op.cancel();
            //$("#dock").hide();
            //$(".dock_view").hide();
            //Designer.contextMenu.destroy();
            //Designer.op.canvasFreeDraggable()
        } else {
            $("#designer_layout").css({
                width: "inherit",
                height: "inherit"
            });
            if($elcon.attr("data-margin") == "true"){
                $elcon.attr("data-margin", false).css({
                    "margin-left": "176px",
                    position: "relative",
                    top: "inherit"
                });
            }
            IS_FULL_SCREEN = false;

            if(randomFull === true){

                if(setDialogType === 2 && !RANDOM_FULL_OK){
                    tsFnDialogInit({
                        width: 860,
                        height: 485,
                        dialog_obj: $("#y-wallpaper-dialog-id"),
                        type: 1
                    });
                }
                // tsFnDialogInit({
                //     width : 850,
                //     height : 283,
                //     dialog_obj: $("#random-wallpaper-preview"),
                //     type : setDialogType
                // });
                $(".priview-background").hide();
            }

            // if(Model.define.page.wallpaper.background.randomLock){
            //     $("#canvas_container").css({
            //         background: "url("+ Model.define.page.wallpaper.background.randomArr[0].url +") no-repeat",
            //         "background-size": "100% 100%"
            //     })
            // }else{
            //     $("#canvas_container").css({
            //         background: "url("+ Model.define.page.wallpaper.background.bigUrl +") no-repeat",
            //         "background-size": "100% 100%"
            //     })
            // }

            serranBg();

            fullexitel.hide();
            $(".shape-panel-btn").show();
            $("#shape_panel").removeClass("readonly");
            $("#designer_viewport").removeClass("readonly");
            Designer.initialize.initLayout();
            Designer.hotkey.init();
            Designer.op.init();
            $("#dock").show();
            if (Dock.currentView != "") {
                Dock.showView(Dock.currentView)
            }
            Designer.contextMenu.init();
            $("#designer").unbind("webkitfullscreenchange");
            $("#designer").unbind("mozfullscreenchange").unbind("fullscreenchange");
            thumbnail.updataSize({
                width: Model.define.page.width,
                height: Model.define.page.height
            });

        }



    }
};
var Navigator = {
    init: function () {
        $("#designer_layout").bind("scroll", function () {
            Navigator.setView()
        });
        $("#navigation_eye").bind("mousedown", function (m) {
            var f = $(this);
            var j = f.position();
            $("#designer_layout").unbind("scroll");
            var g = $("#designer_layout");
            var k = g.scrollTop();
            var d = g.scrollLeft();
            var n = $("#designer_canvas");
            var e = n.width();
            var a = n.height();
            var b = $("#navigation_canvas");
            var i = b.width();
            var c = b.height();
            var l = e / i;
            var h = a / c;
            $(document).bind("mousemove.navigator", function (q) {
                var o = q.pageX - m.pageX;
                var s = q.pageY - m.pageY;
                var r = d + o * l;
                g.scrollLeft(r);
                var p = k + s * h;
                g.scrollTop(p);
                f.css({left: j.left + o, top: j.top + s})
            });
            $(document).bind("mouseup.navigator", function (o) {
                $(document).unbind("mousemove.navigator");
                $(document).unbind("mouseup.navigator");
                Navigator.setView();
                $("#designer_layout").bind("scroll", function () {
                    Navigator.setView();
                })
            })
        });
        $("#navigation_canvas").bind("click", function (l) {
            var m = Utils.getRelativePos(l.pageX, l.pageY, $(this));
            var o = $("#designer_canvas");
            var h = o.width();
            var a = o.height();
            var b = $("#navigation_canvas");
            var k = b.width();
            var c = b.height();
            var n = h / k;
            var j = a / c;
            var g = m.x * n;
            var f = m.y * j;
            var i = $("#designer_layout");
            var d = Designer.config.pageMargin;
            i.scrollLeft(g + d - i.width() / 2);
            i.scrollTop(f + d - i.height() / 2)
        });
        this.setView()
    }, draw: function () {
        if (this.drawNavigationTimeout) {
            window.clearTimeout(this.drawNavigationTimeout)
        }
        this.drawNavigationTimeout = setTimeout(function () {
           try{
               var c = $("#navigation_canvas");
               var r = c[0].getContext("2d");
               r.save();
               r.clearRect(0, 0, c.width(), c.height());
               r.scale(c.width() / Model.define.page.width, c.height() / Model.define.page.height);
               for (var g = 0; g < Model.orderList.length; g++) {
                   var m = Model.orderList[g].id;
                   var l = Model.getShapeById(m);
                   r.save();
                   if (l.name != "linker") {
                       var b = l.props;
                       var a = Utils.getShapeLineStyle(l.lineStyle);
                       r.translate(b.x, b.y);
                       r.translate(b.w / 2, b.h / 2);
                       r.rotate(b.angle);
                       r.translate(-(b.w / 2), -(b.h / 2));
                       r.globalAlpha = l.shapeStyle.alpha;
                       Designer.painter.renderShapePath(r, l)
                   } else {
                       var h = l;
                       var a = Utils.getLinkerLineStyle(h.lineStyle);
                       var q = h.points;
                       var o = h.from;
                       var n = h.to;
                       r.beginPath();
                       r.moveTo(o.x, o.y);
                       if (h.linkerType == "curve") {
                           var f = q[0];
                           var e = q[1];
                           r.bezierCurveTo(f.x, f.y, e.x, e.y, n.x, n.y)
                       } else {
                           for (var d = 0; d < q.length; d++) {
                               var k = q[d];
                               r.lineTo(k.x, k.y)
                           }
                           r.lineTo(n.x, n.y)
                       }
                       r.lineWidth = a.lineWidth;
                       r.strokeStyle = "rgb(" + a.lineColor + ")";
                       r.stroke()
                   }
                   r.restore()
               }
               r.restore();
               Navigator.setView();
               this.drawNavigationTimeout = null
           }catch (e){

           }
        }, 100)
    }, setView: function () {
        var a = $("#navigation_eye");
        var r = $("#designer_layout");
        var u = r.width();
        var d = r.height();
        var b = $("#navigation_canvas");
        var g = b.width();
        var n = b.height();
        var o = $("#designer_canvas");
        var f = o.width();
        var m = o.height();
        var l = Designer.config.pageMargin;
        var h = l - r.scrollLeft();
        var t = h + f;
        if (h < 0) {
            h = 0
        } else {
            if (h > u) {
                h = u
            }
        }
        if (t > u) {
            t = u
        } else {
            if (t < 0) {
                t = 0
            }
        }
        var j = l - r.scrollTop();
        var e = j + m;
        if (j < 0) {
            j = 0
        } else {
            if (j > d) {
                j = d
            }
        }
        if (e > d) {
            e = d
        } else {
            if (e < 0) {
                e = 0
            }
        }
        var i = t - h;
        var p = e - j;
        if (i == 0 || p == 0) {
            //a.hide()
        } else {
            var k = r.scrollLeft() - l;
            if (k < 0) {
                k = 0
            }
            k = k * (g / f);
            var q = r.scrollTop() - l;
            if (q < 0) {
                q = 0
            }
            q = q * (n / m);
            var s = i * (g / f);
            var c = p * (n / m);
            a.css({left: k - 1, top: q - 1, width: s, height: c}).show()
        }
    }
};
(function (c) {
    c.fn.button = function (e) {
        if (typeof e == "string") {
            if (e == "disable") {
                c(this).addClass("disabled");
                c(this).find("input").attr("disabled", true);
            } else {
                if (e == "enable") {
                    c(this).removeClass("disabled");
                    c(this).find("input").attr("disabled", false);
                } else {
                    if (e == "isDisabled") {
                        return c(this).hasClass("disabled");
                    } else {
                        if (e == "isSelected") {
                            return c(this).hasClass("selected");
                        } else {
                            if (e == "unselect") {
                                c(this).removeClass("selected");
                            } else {
                                if (e == "select") {
                                    c(this).addClass("selected");
                                } else {
                                    if (e == "setText") {
                                        c(this).children(".text_content").html(arguments[1]);
                                    } else {
                                        if (e == "setColor") {
                                            c(this).children(".btn_color").css("background-color", "rgb(" + arguments[1] + ")");
                                        } else {
                                            if (e == "getColor") {
                                                var d = c(this).children(".btn_color").css("background-color").replace(/\s/g, "");
                                                return d.substring(4, d.length - 1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return c(this);
        }
        var f = c(this);
        f.unbind("click");
        f.unbind("mousedown");
        if (e.onClick) {
            f.bind("click", function () {
                if (f.button("isDisabled")) {
                    return;
                }
                e.onClick();
            })
        }
        if (e.onMousedown) {
            f.bind("mousedown", function (g) {
                if (f.button("isDisabled")) {
                    return;
                }
                e.onMousedown();
                g.stopPropagation()
            })
        }
    };
    var b = null;
    c.fn.dropdown = function (e) {
        var i = c(this);
        i.find(".ico_selected").remove();
        if (typeof e == "string") {
            if (e == "close") {
                i.hide();
                $(".px-list-custom-err").removeClass("px-list-custom-err");
                $(".px-list-custom-errtext").removeClass("px-list-custom-errtext");
                if(i.attr("id") === "px_list" && $(".px-list-custom").css("display") !== "none"){
                    $(".px-list-custom").hide();
                    var styleobj = {
                        w: "",
                        h: ""
                    },
                    customtext = $(".px-list-custom-click").text().split("*");
                    if(Model.define.page.custom){
                        styleobj = {
                            w: Model.define.page.width,
                            h: Model.define.page.height
                        };
                    }else if(customtext.length > 0){
                        styleobj = {
                            w: customtext[0],
                            h: customtext[1]
                        };
                    }
                    if(!isNaN(styleobj.w)){
                        $(".custom-w").val(styleobj.w);
                    }
                    $(".custom-h").val(styleobj.h);
                }
                b.target.removeClass("selected");
                c(document).unbind("mousedown.ui_dropdown");
                b = null;
            } else {
                if (e == "select") {
                    arguments[1].prepend("<div class='ico ico_selected'></div>");
                }
            }
            return;
        }
        if (b != null) {
            b.menu.dropdown("close");
        }
        var i = c(this);
        var d = e.target;
        b = {target: d, menu: i};
        var h = d.offset();
        d.addClass("selected");
        i.show();
        var g;
        if (e.position == "center") {
            g = h.left + d.outerWidth() / 2 - i.outerWidth() / 2;
        } else {
            if (e.position == "right") {
                g = h.left + d.outerWidth() - i.outerWidth();
            } else {
                g = h.left;
            }
        }
        var f = h.top + d.outerHeight();
        if (f + i.outerHeight() > c(window).height()) {
            f = c(window).height() - i.outerHeight();
        }
        i.css({top: f, left: g});
        if (typeof e.zindex != "undefined") {
            i.css("z-index", e.zindex);
        }
        i.unbind("mousedown").bind("mousedown", function (j) {
            j.stopPropagation();
        });
        if (typeof e.bind == "undefined" || e.bind == true) {
            i.find("li:not(.devider,.menu_text)").unbind().bind("click", function (event) {
                var j = c(this),
                    returndata = false;
                if (!j.menuitem("isDisabled") && j.children(".extend_menu").length == 0) {
                    if (e.onSelect) {
                        returndata = e.onSelect(j, event);
                    }
                    if(returndata === "none") return;
                    i.dropdown("close");
                }
            });
        }
        c(document).bind("mousedown.ui_dropdown", function () {
            i.dropdown("close");
        });
    };
    c.fn.menuitem = function (d) {
        var e = c(this);
        if (typeof d == "string") {
            if (d == "disable") {
                e.addClass("disabled");
            } else {
                if (d == "enable") {
                    e.removeClass("disabled");
                } else {
                    if (d == "isDisabled") {
                        return e.hasClass("disabled");
                    } else {
                        if (d == "isSelected") {
                            return e.children(".ico_selected").length > 0;
                        } else {
                            if (d == "unselect") {
                                return e.children(".ico_selected").remove();
                            } else {
                                if (d == "select") {
                                    return e.prepend("<div class='ico ico_selected'></div>");
                                }
                            }
                        }
                    }
                }
            }
        }
    };
})(jQuery);